/*
 * 
 */
package mt.dummy;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import mt.comm.ServerMessage;
import mt.comm.ServerSideMessage.Type;

import org.junit.Before;
import org.junit.Test;


/**
 * Test Class ServerCommDummy.
 *
 * @author Grupo 5
 */

public class ServerCommDummyTest {

	/** The server. */
	ServerCommDummy server;

	/** The client3. */
	ClientCommDummy client, client2, client3;

	/**
	 * Sets the up.
	 */
	@Before
	public void setUp() {
		server = new ServerCommDummy();

		client2 = new ClientCommDummy();
		client3 = new ClientCommDummy();

	}

	/**
	 * Test start.
	 */
	@Test
	public void testStart() {
		server.start();

		//client.connect("127.0.0.1", "root");

	}

	/**
	 * Test has next message.
	 */
	@Test
	public void testhasNextMessage() {

		assertFalse(server.hasNextMessage());

		assertFalse(server.clientIsConnected("joao"));
		ServerMessage msg = new ServerMessage(Type.CONNECTED, "user", null);
		server.getMessages().add(msg);
		server.addNewClient("user", null, msg);
		assertTrue(server.clientIsConnected("user"));

		assertTrue(server.hasNextMessage());

	}

	/**
	 * Test get next message.
	 */
	@Test
	public void testGetNextMessage() {
		assertFalse(server.clientIsConnected("joao"));
		ServerMessage msg = new ServerMessage(Type.CONNECTED, "user", null);
		server.getMessages().add(msg);
		server.addNewClient("user", null, msg);
		assertTrue(server.clientIsConnected("user"));

		server.getNextMessage();
	}

	/**
	 * Test send error.
	 */
	@Test
	public void testSendError() {
		assertFalse(server.clientIsConnected("joao"));
		ServerMessage msg = new ServerMessage(Type.CONNECTED, "user", null);
		server.getMessages().add(msg);
		server.addNewClient("user", null, msg);
		assertTrue(server.clientIsConnected("user"));

		server.sendError("user", null);
	}

	/**
	 * Test client is connected.
	 */
	@Test
	public void testClientIsConnected() {

		assertFalse(server.clientIsConnected("user"));
		ServerMessage msg = new ServerMessage(Type.CONNECTED, "user", null);
		server.getMessages().add(msg);
		server.addNewClient("user", null, msg);
		assertTrue(server.clientIsConnected("user"));

	}

	/**
	 * Test disconnect client.
	 */
	@Test
	public void testDisconnectClient() {
		server.disconnectClient("user");

	}

	/**
	 * Test send order.
	 */
	@Test
	public void testSendOrder() {
		assertFalse(server.clientIsConnected("user"));
		ServerMessage msg = new ServerMessage(Type.CONNECTED, "user", null);
		server.getMessages().add(msg);
		server.addNewClient("user", null, msg);
		assertTrue(server.clientIsConnected("user"));

		server.sendOrder("joao", null);

	}

	/**
	 * Test add new client.
	 */
	@Test
	public void testAddNewClient() {

		assertFalse(server.clientIsConnected("user"));
		ServerMessage msg = new ServerMessage(Type.CONNECTED, "user", null);
		server.getMessages().add(msg);
		server.addNewClient("user", null, msg);
		assertTrue(server.clientIsConnected("user"));

		server.addNewClient("user", null, msg);

	}

	/**
	 * Test add new message.
	 */
	@Test
	public void testAddNewMessage() {
		server.addNewMessage(new ServerMessage(Type.DISCONNECTED, "user", null));
	}

}
